

template <typename T>
class UnionFindSet
{
public:
    UnionFindSet()
    {
    }

    template <typename TContainer>
    UnionFindSet(const TContainer &container)
    {
        auto begin = container.begin();
        auto end = container.end();
        while (begin != end)
        {
            m[*begin] = *begin;
            ++begin;
        }
    }

    void insert(const T &value)
    {
        if (!m.count(value))
        {
            m[value] = value;
        }
    }

    bool exist(const T &value)
    {
        return m.count(value);
    }

    T find(const T &value)
    {
        if (m[value] == value)
        {
            return value;
        }
        return m[value] = find(m[value]);
    }

    void join(const T &value1, const T &value2)
    {
        if (find(value1) != find(value2))
        {
            m[find(value1)] = find(value2);
        }
    }

private:
    std::unordered_map<T, T> m;
};

class Solution
{
public:
    vector<int> findAllPeople(int n, vector<vector<int>> &meetings, int firstPerson)
    {
        UnionFindSet<int> ufs;
        for (int i = 0; i < n; ++i)
        {
            ufs.insert(i);
        }
        ufs.join(0, firstPerson);

        sort(meetings.begin(), meetings.end(), [](auto &meeting1, auto &meeting2)
             { return meeting1[2] < meeting2[2]; });

        auto meeting = meetings.begin();
        auto nextMeeting = meetings.begin() + 1;
        while (meeting != meetings.end())
        {
            unordered_set<int> experts;
            experts.insert((*meeting)[0]);
            experts.insert((*meeting)[1]);
            while (nextMeeting != meetings.end())
            {
                if ((*nextMeeting)[2] == (*meeting)[2])
                {
                    experts.insert((*nextMeeting)[0]);
                    experts.insert((*nextMeeting)[1]);
                    ++nextMeeting;
                }
                else
                {
                    break;
                }
            }

            UnionFindSet<int> expertGroups(experts);
            expertGroups.insert(0);

            bool existKnown = false;
            for (auto m = meeting; m != nextMeeting; ++m)
            {
                int expert1 = (*m)[0];
                int expert2 = (*m)[1];
                expertGroups.join(expert1, expert2);
                if ((ufs.find(expert1) == ufs.find(0) || ufs.find(expert2) == ufs.find(0)))
                {
                    expertGroups.join(expert1, 0);
                    existKnown = true;
                }
            }

            if (existKnown)
            {
                for (int expert : experts)
                {
                    if (expertGroups.find(expert) == expertGroups.find(0))
                    {
                        ufs.join(expert, 0);
                    }
                }
            }
            meeting = nextMeeting;
        }

        vector<int> result;
        for (int expert = 0; expert < n; ++expert)
        {
            if (ufs.find(expert) == ufs.find(0))
            {
                result.push_back(expert);
            }
        }
        return result;
    }
};